package com.yxy.springcloud.framework.unionauth.controller;

import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.unionauth.DTO.*;
import com.yxy.springcloud.framework.unionauth.model.AppDataDic;
import com.yxy.springcloud.framework.unionauth.service.DataDicService;
import com.yxy.springcloud.framework.unionauth.service.UserService;
import com.yxy.springcloud.framework.unionauth.utils.CheckAppPermission;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yuxiaoyang
 * app 数据字典相关接口
 */
@RestController
@RequestMapping("/data-dic")
public class DataDicController {
  @Resource
  private DataDicService functionService;

  @PostMapping("/list")
  public RestResponse getList(@RequestBody DataDicQueryDto query) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getList(query));
    return restResponse;
  }

  @PostMapping("/createOrUpdate")
  public RestResponse createOrUpdate(@RequestBody AppDataDic inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate(inputDto));
    return restResponse;
  }

  @DeleteMapping("/{id}")
  public RestResponse delete(@PathVariable("id") long id) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.delete(id);
    return restResponse;
  }

  // 供app调用,获取app的所有数据字典
  @GetMapping("/getDataDicByApp")
  public RestResponse getDataDicByApp(@RequestParam("appName") String appName) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getDataDicByApp(appName));
    return restResponse;
  }

  // #########字典具体项的增删改查功能############
  // 字典具体项的查找功能
  @PostMapping("/{id}/items")
  public RestResponse getDicItems(@PathVariable("id") long id, @RequestBody DataDicItemsQueryDto query) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getDicItems(id, query));
    return restResponse;
  }

  // 字典具体项的增加和修改
  @PostMapping("/{id}/items/createOrUpdate")
  public RestResponse createOrUpdateDicItems(@PathVariable("id") long id, @RequestBody DataDicItemsCreateDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdateDicItems(id, inputDto));
    return restResponse;
  }

  // 字典具体项的删除接口
  @DeleteMapping("/items/{itemId}")
  public RestResponse deleteItem(@PathVariable("itemId") long itemId) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.deleteItem(itemId);
    return restResponse;
  }

  // 供app后台调用, 根据字典具体项的主键id查询具体项的信息
  @GetMapping("/getDataDicDetailById")
  @CheckAppPermission
  public RestResponse getDataDicDetailById(@RequestParam("id") Long id) {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(functionService.getDataDicDetailById(id));
    return restResponse;
  }

  /**
   * 供app后台调用, 根据字典具体项的value查询具体项的信息
   * @param appCode 即app后台的spring.application.name的值
   * @param value 格式: DIC_GENDER.男
   * @return
   */
  @GetMapping("/getDataDicDetailByValue")
  @CheckAppPermission
  public RestResponse getDataDicDetailByValue(@RequestParam("appCode") String appCode, @RequestParam("value") String value) {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(functionService.getDataDicDetailByValue(appCode, value));
    return restResponse;
  }
}
